/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007-2008 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.projects.toolbox_basics_project.bean.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;

import com.genia.toolbox.persistence.exception.PersistenceException;
import com.genia.toolbox.projects.toolbox_basics_project.bean.InitializationParameterHandler;
import com.genia.toolbox.projects.toolbox_basics_project.bean.PersistableApplicationParameter;
import com.genia.toolbox.projects.toolbox_basics_project.bean.model.Parameter;
import com.genia.toolbox.projects.toolbox_basics_project.bean.model.PrimitiveParameter;

/**
 * Default implementation of {@link PersistableApplicationParameterHandler}.
 */
public class DefaultPersistableApplicationParameterHandler
    extends AbstractDefaultParameterHandler
    implements InitializationParameterHandler<Object>

{
  /**
   * the <code>Log</code> variabe.
   */
  private static final Logger LOGGER = LoggerFactory.getLogger(DefaultPersistableApplicationParameterHandler.class);



  /**
   * intialize the given {@link PersistableApplicationParameter}.
   * 
   * @param persistableApplicationParameter
   *          the {@link PersistableApplicationParameter} to initialize
   * @throws PersistenceException
   *           when an error occured
   * @see com.genia.toolbox.projects.toolbox_basics_project.bean.PersistableApplicationParameterHandler#initValue(com.genia.toolbox.projects.toolbox_basics_project.bean.impl.PersistableApplicationParameter)
   */
  @Transactional(rollbackFor = PersistenceException.class)
  public void initValue(PersistableApplicationParameter<Object> persistableApplicationParameter)
      throws PersistenceException
  {
    Parameter parameter = getParameterManager().getParameter(persistableApplicationParameter.getReferenceName());
    if (parameter == null) {
      return;
    }
    if (parameter instanceof PrimitiveParameter) {
      String value = ((PrimitiveParameter) parameter).getValue();
      try {
        Object castedValue = getValue(persistableApplicationParameter, value);
        persistableApplicationParameter.setValue(castedValue);
        return;
      }
      catch (Exception e) {
        LOGGER.error(e.getMessage(), e);
      }
    }
    getParameterManager().delete(parameter);
  }

}
